/*
 * @Descripttion: 
 * @version: 
 * @Author: turingchang
 * @Date: 2020-09-02 17:14:25
 * @LastEditors: turingchang
 * @LastEditTime: 2020-09-08 14:11:38
 */
/* indent size: 2 */

module.exports = app => {
  const DataTypes = app.Sequelize;

  const Model = app.model.define('role', {
    id: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      primaryKey: true,
      autoIncrement: true
    },
    key: {
      type: DataTypes.STRING(50),
      allowNull: false,
      defaultValue: '',
      get(){
        var value =this.getDataValue('key');
        if(app.utils.validator.isNull(value)){
          value ='';
        }
        return value;
      }
    },
    name: {
      type: DataTypes.STRING(20),
      allowNull: false,
      defaultValue: '',
      get(){
        var value =this.getDataValue('name');
        if(app.utils.validator.isNull(value)){
          value ='';
        }
        return value;
      }
    },
    status: {
      type: DataTypes.STRING(20),
      allowNull: true,
      defaultValue: 'normal',
      get(){
        var value =this.getDataValue('status');
        if(app.utils.validator.isNull(value)){
          value ='';
        }
        return value;
      }
    },
    system_built_in: {
      type: DataTypes.STRING(20),
      allowNull: true,
      defaultValue: 'un_built_in',
      get(){
        var value =this.getDataValue('system_built_in');
        if(app.utils.validator.isNull(value)){
          value ='';
        }
        return value;
      }
    },
    describe: {
      type: DataTypes.STRING(255),
      allowNull: true,
      get(){
        var value =this.getDataValue('describe');
        if(app.utils.validator.isNull(value)){
          value ='';
        }
        return value;
      }
    },
    create_time: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      defaultValue:  Math.round(new Date().getTime()/1000).toString(),
      get(){
        var value =this.getDataValue('create_time');
        if(app.utils.validator.isNull(value)){
          value ='0';
        }
        return value;
      }
    },
    update_time: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      defaultValue:  Math.round(new Date().getTime()/1000).toString(),
      get(){
        var value =this.getDataValue('update_time');
        if(app.utils.validator.isNull(value)){
          value ='0';
        }
        return value;
      }
    },
    delete_time: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      defaultValue: '0',
      get(){
        var value =this.getDataValue('delete_time');
        if(app.utils.validator.isNull(value)){
          value ='0';
        }
        return value;
      }
    }
  }, {
    tableName: 'yfb_admin_role',
    createdAt: "create_time",
    updatedAt: 'update_time',
    deletedAt: 'delete_time', 
    paranoid: true,
  });

  Model.associate = function() {
    // app.model.Role.hasMany(app.model.Admin,{foreignKey: 'role_id', targetKey: 'id'});
    app.model.Admin.Role.hasMany(app.model.Admin.Admin);
    app.model.Admin.Role.belongsToMany(app.model.Admin.Permission, { through: 'role_permission' });
  }

  return Model;
};
